<?php
namespace Home\Controller;
use Util\AccessController;
use Home\Util\lib\YZOauthClient;
use Home\Util\lib\YZTokenClient;

/*
 * 微信登录拦截模块
 * 执行后S保存微信用户信息
 *
 */

class LoginController extends AccessController {

    public function eixt() {
        $this->S('_REMOVE_');
        redirect('Index/index');
    }

    public function index() {
        $YZ_URL = C('YZ_URL');
        $URL = C('URL_ROOT');
        $CLIENT_ID = C('CLIENT_ID');
        header("Content-type:text/html;charset=utf-8");
        ini_set('date.timezone', 'Asia/Shanghai');
        Header("Location:{$YZ_URL}?client_id={$CLIENT_ID}&response_type=code&state=teststate&redirect_uri={$URL}");

    }

    public function Handle() {
        $CLIENT_ID = C('CLIENT_ID');
        $CLIENT_SECRET = C('CLIENT_SECRET');
        $redirectUrl = C('URL_ROOT');
        if ($_SESSION['access_token'] == '') {
            $token = new YZOauthClient($CLIENT_ID, $CLIENT_SECRET);
            $keys = array();
            $type = 'code';//如要刷新access_token，这里的值为refresh_token
            $keys['code'] = $_GET['code'];//如要刷新access_token，这里为$keys['refresh_token']
            $keys['redirect_uri'] = $redirectUrl;

            $access = $token->getToken($type, $keys);
            $access_token = $access['access_token'];
        } else {
            $access_token = $_SESSION['access_token'];
        }

        $this->S(array('access_token' => $access_token));
        $this->redirect('Index/index');
    }
}

?>
